const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        tips: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        app.func.onPageLoad(this, options);
        this.list(0);
    },

    list: function (offset, length = 20) {
        wx.showLoading();
        app.func.getPromise(`/list/cardplan/${offset}?length=${length}`)
            .then(([code, res]) => {
                if (code == 200) {
                    if (res.data.length > 0) {
                        if (res.data.length < length) {
                            this.tips(() => wx.hideLoading() );
                        }
                        if (offset == 0) {
                            this.fristdata(res.data, () => wx.hideLoading());
                        } else {
                            this.adddata(res.data, () => wx.hideLoading());
                        }
                    } else {
                        this.tips(() => wx.hideLoading());
                    }
                } else {
                    app.func.toastPromise(res.message)
                }
            }).finally(() => wx.hideLoading())
    },

    fristdata: function (data, cd) {
        console.log(data);
        this.setData({list: data}, cd);
    },
    adddata: function(data, cd){
        let list = this.data.list;
        list = list.concat(data);
        this.setData({list: list}, cd);
    },
    tips: function(cd){
        this.setData({tips: true},cd);
    },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if(this.data.tips == true){
             // llll
        }else{
            let offset = this.data.list.length;
            this.list(offset);
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
